<?php
/**
 * @version		$Id: search.php 10752 2008-08-23 01:53:31Z eddieajau $
 * @package		Joomla
 * @subpackage	Search
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Search Component Search Model
 *
 * @package		Joomla
 * @subpackage	Search
 * @since 1.5
 */
class SimModelSim extends JModel
{
    /**
     * Sezrch data array
     * @var array
     */
	var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_limit = 30;
	var $_limitstart = 0;
	
	
	public function __construct(){
		
		parent::__construct();
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$limitstart = JRequest::getInt('limitstart');
		
		$this->_limitstart = empty($limitstart) ? 0 : $limitstart;
		
		$this->setState('limit', $this->_limit);
		$this->setState('limitstart', $this->_limitstart);
	}
	
    function &getData()
    {
        
		if (empty($this->_data)){
		
            $minprice = JRequest::getVar("minprice");
            if(strlen($minprice)>3){
                $minprice = substr($minprice,0,strlen($minprice)-3);
            }
            $maxprice = JRequest::getVar("maxprice");
            if(strlen($maxprice)>3){
                $maxprice = substr($maxprice,0,strlen($maxprice)-3);
            }
            $searchword = JRequest::getVar("searchword");
            $query = "select S.*,P.provider_name,A.admin_name from #__sim S,#__sim_provider P,#__sim_admin A where S.provider_id=P.provider_id and S.admin_id=A.admin_id and S.del_flag=0 and S.price >".$minprice.' and S.price<'.$maxprice.' ';
            if(JRequest::getVar("provider")){
                $provider = JRequest::getVar("provider");
                $query=$query.' and S.provider_id='.$provider;
            }
            if(JRequest::getVar("category")){
                $category = JRequest::getVar("category");
                $query=$query.' and S.categori_id='.$category;
            }
			$query = $query.' order by S.price desc';
		
            $rows = $this->_getList( $query );
            if($searchword==""){
                
				$this->_data = $rows;
            }
            else{
                
				$length = count($rows);
                foreach ($rows as $row)
                {
                    $sim_numb = str_replace(" ","",$row->sim_numb);
					$sim_numb = str_replace(".","",$sim_numb);
					$searchword= str_replace(" ","",$searchword);
					$searchword= str_replace(".","",$searchword);
                    if(substr_count($sim_numb, $searchword)>0){
                        $this->_data[]=$row;
                    }
                }
            }
        }
		
        return $this->_data;
    }
	
	public function getResult(){
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$query = $this->_buildQuery();
		//echo $query; exit;
		if (empty($this->_data)){
		
			$this->_db->setQuery( $query, $this->_limitstart, $this->_limit );
			$this->_data = $this->_db->loadObjectList();
		}
		
		$data = $this->_data;
		
		return $data;
		
	}
	
	public function _buildQuery(){
		
		//echo "<pre>";print_r(JRequest::get($_GET));exit;
		
		$get = JRequest::get($_GET);
		
		$dk = "";
		
		if( count($get["smang"]) > 1 && ($get["smang"][0] != "0") ) {
			
			$str = "";
			
			$str .= " (P.provider_id = '".$get["smang"][0]."') ";
			
			for($i = 1; $i < count($get["smang"]); $i++){
			
				$str .= " OR (P.provider_id = '".$get["smang"][$i]."') ";
			}
			$dk .= " AND ".($str);
		}
		else if( count($get["smang"]) == 1 && ($get["smang"][0] != "0") ) {
			
			$str .= " (P.provider_id = '".$get["smang"][0]."') ";
			
			$dk .= " AND ".( $str );
			
		}
		
		
		
		if( ($get["minprice"] != "")  &&  ($get["maxprice"] != "") && (strlen($get["minprice"]) > 3) && (strlen($get["maxprice"]) >3) ){
			
			settype($get["minprice"],"bigint"); settype($get["maxprice"],"bigint");
			
			$get["minprice"] = substr($get["minprice"],0,strlen($get["minprice"]) - 3 );
			
			$get["maxprice"] = substr($get["maxprice"],0,strlen($get["maxprice"])-3);
				
			$dk .= " AND ( S.price > '".$get["minprice"]."' ) AND ( S.price < '".$get["maxprice"]."' ) ";
			
		}
		
		if($get["Textsim"] != ""){
			
			$dk .= " AND ( S.sim_numb LIKE '%".$get["Textsim"]."%' ) ";
		}
		
		if( count($get["n"]) > 0 ){
			
			for($i=0; $i < count($get["n"]); $i++){
			
				$dk .= " AND ( S.sim_numb NOT LIKE '%".$get["n"][$i]."%' ) ";
			}
			
		}

		if( count($get["sdang"]) > 1 && ($get["sdang"][0] != "0") ) {
			
			$str = "";
			$str .= " (C.categori_id = '".$get["sdang"][0]."') ";
			
			for($i = 0; $i < count($get["sdang"]); $i++){
			
				$str .= " OR ( C.categori_id = '".$get["sdang"][$i]."' ) ";
			}
			$dk .= " AND ".($str);
		}
		else if( count($get["sdang"]) == 1 && ($get["sdang"][0] != "0") ) {
			
			$str .= " (C.categori_id = '".$get["sdang"][0]."') ";
			$dk .= " AND ".( $str );
		}
		
		if($get["ssapxep"] == "2") {
		
			$dk .= " ORDER BY S.price ASC ";
		}else { $dk .= " ORDER BY S.price DESC "; }
		
		//echo $dk;exit;
		
		$query = "SELECT S.*,P.provider_name
				
				FROM 	#__sim  AS S
						LEFT JOIN #__sim_provider AS P ON S.provider_id = P.provider_id
						LEFT JOIN #__sim_categories AS C ON S.categori_id = C.categori_id
				WHERE 1 ";
					  
		//echo $query;exit;
		
		$query = $query.$dk;
		
		return $query;
		
		
	}
	public function getTotal(){
		
		if (empty($this->_total)){
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	public function getPagination(){
	
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->_limitstart, $this->_limit );
		}
		return $this->_pagination;
	}
	
}
